package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BCaseConference;
import com.ruoyi.system.domain.BUserSign;
import com.ruoyi.system.domain.request.SignRequest;
import com.ruoyi.system.service.BCaseConferenceService;
import com.ruoyi.system.mapper.BCaseConferenceMapper;
import com.ruoyi.system.service.BUserSignService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author yangxingze
* @description 针对表【b_case_conference(案例研讨会关联表)】的数据库操作Service实现
* @createDate 2024-10-11 23:30:55
*/
@Service
public class BCaseConferenceServiceImpl extends ServiceImpl<BCaseConferenceMapper, BCaseConference>
    implements BCaseConferenceService{

    @Autowired
    BUserSignService userSignService;

    /**
     * 报名会议
     * @param request
     * @return
     */
    @Override
    public boolean sign(SignRequest request) {
        Long userId = SecurityUtils.getUserId();
        Long conferenceId= request.getConferenceId();
        BUserSign userSign = userSignService.getOne(new QueryWrapper<BUserSign>().lambda()
                .eq(BUserSign::getUserId,userId)
                .eq(BUserSign::getConferenceId,conferenceId));
        if(ObjectUtils.isNotEmpty(userSign)){
            throw new ServiceException("您已经报名了该会议，请勿重复报名!");
        }
        userSign = new BUserSign();
        userSign.setConferenceId(conferenceId);
        userSign.setUserId(userId);
        userSign.setCreateTime(new Date());
        return userSignService.save(userSign);
    }

    /**
     * 创建会议案例关联
     * @param conferenceId
     * @param caseIds
     * @return
     */
    @Override
    public boolean createCaseConference(Long conferenceId, List<Long> caseIds) {
        //删除关联数据
        this.remove(new QueryWrapper<BCaseConference>().lambda()
                .eq(BCaseConference::getConferenceId,conferenceId));
        //创建关联数据
        for(Long caseId :caseIds){
            BCaseConference conference = new BCaseConference();
            conference.setCaseId(caseId);
            conference.setConferenceId(conferenceId);
            save(conference);
        }
        return true;
    }
}




